package uk.org.landeg.euler.problems;

import java.util.ArrayList;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;
import uk.org.landeg.euler.problems.lib.NumbersLib;
import uk.org.landeg.euler.problems.lib.PrimeLib;

public class Problem030 extends AbstractProblem{
	ArrayList<Long> primes = PrimeLib.seqPrimes(10000L);
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem030());
		launcher.solve();		
	}
	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return "Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.";
	}
	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 30;
	}
	
	@Override
	public Object solve() {
		// TODO Auto-generated method stub
		int [] digits;
		long total = 0;
		long total5;
		int [] fifthPowers = new int [10];
		for (int i = 0 ;i < fifthPowers.length ; i++) {
			fifthPowers[i] = i*i*i*i*i;
		}
		
		int maxValue = (int)Math.pow(9, 5) * 5;
		notes.add("expect upper bounds to be 9^5 * 5 = " + maxValue);
		for (int n = 2 ; n < maxValue ; n++) {
			total5 = 0;
			digits = NumbersLib.getDigits(n);
			for (int d : digits) {
				total5 += fifthPowers[d];
				if (total5 > n) {
					break ;
				}
			}
			if (total5 == n) {
				System.out.println(n);
				total += n;
			}
		}
		return total;
	}
}
